﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Mimoza.Common
{
    public class UpdatesFileInfo
    {

        private string m_name;
        private string m_description;
        private string m_ModelName;
        private string m_OSName;
        private string m_version;
        private long m_updateSize;

        public UpdatesFileInfo()
        {
        }

        public UpdatesFileInfo(Stream stream)
        {
            ReadInfo(stream);
        }

        public UpdatesFileInfo(string fileName)
        {
            ReadInfo(fileName);
            //TestInit();
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public string Description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        public string OSName
        {
            get { return m_OSName; }
            set { m_OSName = value; }
        }

        public string ModelName
        {
            get { return m_ModelName; }
            set { m_ModelName = value; }
        }

        public string Version
        {
            get { return m_version; }
            set { m_version = value; }
        }

        public long Size
        {
            get { return m_updateSize; }
            set { m_updateSize = value; }
        }

        public void WriteInfo(Stream fs)
        {
            FieldsFile.WriteField(Name, fs);
            FieldsFile.WriteField(OSName, fs);
            FieldsFile.WriteField(ModelName, fs);
            FieldsFile.WriteField(Version, fs);
            FieldsFile.WriteField(Description, fs);
            FieldsFile.WriteField(Size.ToString(), fs);
            fs.Flush();
        }

        public void ReadInfo(string fileName)
        {
            using (FileStream reader = File.OpenRead(fileName))
            {
                ReadInfo(reader);
            }
        }

        public void ReadInfo(Stream reader)
        {
            Name = FieldsFile.ReadField(reader);
            OSName = FieldsFile.ReadField(reader);
            ModelName = FieldsFile.ReadField(reader);
            Version = FieldsFile.ReadField(reader);
            Description = FieldsFile.ReadField(reader);
            Size = long.Parse(FieldsFile.ReadField(reader));
        }

        void TestInit()
        {
            OSName = "Windows CE 5.0";
            ModelName = "L66-CE5";
            Version = "1.2.0.RC2";
        }
    }
}
